﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataContracts;
using System.ServiceModel;

namespace Presention
{
    public partial class EditUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Bind();
            }
        }
        private void Bind()
        {

            string uid = Request["UserId"];
            if (uid == null)
            {
                Response.Write("<script>alert('Failed ')</script>");

            }

            int userid = int.Parse(uid);
            using (ServiceAgent UserServiceAgent = new ServiceAgent())
            {
                UserDTO user = UserServiceAgent.ServiceClient.GetUserById(userid);

                EditFirstNameTextBox.Text = user.FirstName;
                EditLastNameTextBox.Text = user.LastName;
                EditDepartmentDropDownList.SelectedValue = user.Department.DepartmentId.ToString();
                EditRoleDropDownList.SelectedValue = user.Role.RoleId.ToString();

                EditDepartmentDropDownList.DataSource = UserServiceAgent.ServiceClient.GetAllDepartment();

                EditDepartmentDropDownList.DataTextField = "DepartmentName";
                EditDepartmentDropDownList.DataValueField = "DepartmentId";
                EditDepartmentDropDownList.DataBind();

                EditRoleDropDownList.DataSource = UserServiceAgent.ServiceClient.GetAllUserRole();
                EditRoleDropDownList.DataTextField = "RoleName";
                EditRoleDropDownList.DataValueField = "RoleId";
                EditRoleDropDownList.DataBind();
            }
        }

        /// <summary>
        /// Update User Details
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void EditUpdateButton_Click(object sender, EventArgs e)
        {
            UserDTO User = new UserDTO();

            User.UserId = int.Parse(Request.QueryString["UserId"]);
            User.FirstName = EditFirstNameTextBox.Text;
            User.LastName = EditLastNameTextBox.Text;
            User.Department = new DepartmentDTO();
            User.Department.DepartmentId = int.Parse(EditDepartmentDropDownList.SelectedValue);
            User.Role = new RoleDTO();
            User.Role.RoleId = int.Parse(EditRoleDropDownList.SelectedValue);
            using (ServiceAgent UserServiceAgent = new ServiceAgent())
            {
                try
                {
                    UserServiceAgent.ServiceClient.UpdateUser(User);
                    Response.Write("<script>alert('Modify successfully');window.location.href='UserList.aspx'</script>");
                }
                catch (FaultException ex)
                {
                    Response.Write("<script>alert('" + ex.Message + "')</script>");
                    Bind();
                }
            }

        }
    }

}

